<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="程序、进程与线程程序、进程和线程是计算机中不同概念，它们在执行和管理任务的过程中扮演着不同的角色。  程序（Program）：程序是指一组按照特定顺序组织的指令，用于告诉计算机如何执行某种任务。程序通常以源代码的形式编写，通过编译或解释等过程转换为机器可以理解和执行的形式。 进程（Process）：进程是一个正在执行的程序的实例。当程序被加载到内存中并执行时，操作系统会为该程序创建一个进程。每个进">
<meta property="og:type" content="article">
<meta property="og:title" content="Java：多线程">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/06/13/Java%EF%BC%9A%E5%A4%9A%E7%BA%BF%E7%A8%8B/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="程序、进程与线程程序、进程和线程是计算机中不同概念，它们在执行和管理任务的过程中扮演着不同的角色。  程序（Program）：程序是指一组按照特定顺序组织的指令，用于告诉计算机如何执行某种任务。程序通常以源代码的形式编写，通过编译或解释等过程转换为机器可以理解和执行的形式。 进程（Process）：进程是一个正在执行的程序的实例。当程序被加载到内存中并执行时，操作系统会为该程序创建一个进程。每个进">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-06-13T13:24:53.000Z">
<meta property="article:modified_time" content="2023-06-27T12:23:36.847Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-Java基础 -多线程 -Java多线程">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/06/13/Java%EF%BC%9A%E5%A4%9A%E7%BA%BF%E7%A8%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/06/13/Java%EF%BC%9A%E5%A4%9A%E7%BA%BF%E7%A8%8B/","path":"2023/06/13/Java：多线程/","title":"Java：多线程"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java：多线程 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%A8%8B%E5%BA%8F%E3%80%81%E8%BF%9B%E7%A8%8B%E4%B8%8E%E7%BA%BF%E7%A8%8B"><span class="nav-text">程序、进程与线程</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="nav-text">创建线程的方式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Thread%E7%B1%BB%E7%9A%84%E5%AD%90%E7%B1%BB%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="nav-text">使用Thread类的子类创建线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Runnable%E6%8E%A5%E5%8F%A3%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="nav-text">使用Runnable接口创建线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E5%8C%BF%E5%90%8D%E7%B1%BB%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="nav-text">使用匿名类创建线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="nav-text">使用Lambda表达式创建线程</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%8E%E7%BA%BF%E7%A8%8B%E7%9B%B8%E5%85%B3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">与线程相关的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E7%BA%BF%E7%A8%8B"><span class="nav-text">获取当前线程</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81"><span class="nav-text">线程的状态</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B0%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="nav-text">新建线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%AF%E8%BF%90%E8%A1%8C%E7%BA%BF%E7%A8%8B"><span class="nav-text">可运行线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%98%BB%E5%A1%9E%E5%92%8C%E7%AD%89%E5%BE%85%E7%BA%BF%E7%A8%8B"><span class="nav-text">阻塞和等待线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%88%E6%AD%A2%E7%BA%BF%E7%A8%8B"><span class="nav-text">终止线程</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E5%B1%9E%E6%80%A7"><span class="nav-text">线程属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%AD%E6%96%AD%E7%BA%BF%E7%A8%8B"><span class="nav-text">中断线程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%86%E4%B8%AD%E6%96%AD%E8%A7%A3%E9%87%8A%E4%B8%BA%E7%BB%88%E6%AD%A2"><span class="nav-text">将中断解释为终止</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B0%83%E7%94%A8sleep%E5%B0%B1%E4%B8%8D%E8%A6%81%E6%A3%80%E6%B5%8B%E4%B8%AD%E6%96%AD%E7%8A%B6%E6%80%81"><span class="nav-text">调用sleep就不要检测中断状态</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E5%90%8D"><span class="nav-text">线程名</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%AA%E6%8D%95%E8%8E%B7%E5%BC%82%E5%B8%B8%E7%9A%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="nav-text">未捕获异常的处理器</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%90%8C%E6%AD%A5"><span class="nav-text">同步</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%94%81%E5%AF%B9%E8%B1%A1"><span class="nav-text">锁对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%81%E7%9A%84%E4%BD%BF%E7%94%A8%E7%BB%93%E6%9E%84"><span class="nav-text">锁的使用结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%81%E7%9A%84%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B"><span class="nav-text">锁的使用举例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%94%81%E7%9A%84%E5%B5%8C%E5%A5%97%E8%B0%83%E7%94%A8%E4%B8%8E%E6%8C%81%E6%9C%89%E8%AE%A1%E6%95%B0"><span class="nav-text">锁的嵌套调用与持有计数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="nav-text">条件对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#synchronized%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-text">synchronized关键字</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/06/13/Java%EF%BC%9A%E5%A4%9A%E7%BA%BF%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java：多线程 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java：多线程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-13 21:24:53" itemprop="dateCreated datePublished" datetime="2023-06-13T21:24:53+08:00">2023-06-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-06-27 20:23:36" itemprop="dateModified" datetime="2023-06-27T20:23:36+08:00">2023-06-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-后端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="程序、进程与线程"><a href="#程序、进程与线程" class="headerlink" title="程序、进程与线程"></a>程序、进程与线程</h1><p>程序、进程和线程是计算机中不同概念，它们在执行和管理任务的过程中扮演着不同的角色。</p>
<ol>
<li>程序（Program）：程序是指一组按照特定顺序组织的指令，用于告诉计算机如何执行某种任务。程序通常以源代码的形式编写，通过编译或解释等过程转换为机器可以理解和执行的形式。</li>
<li>进程（Process）：进程是一个正在执行的程序的实例。当程序被加载到内存中并执行时，操作系统会为该程序创建一个进程。每个进程都有自己的内存空间、寄存器集合、文件和资源。进程是计算机中任务执行和资源管理的基本单位。</li>
<li>线程（Thread）：线程是进程中的一个执行路径。一个进程可以包含多个线程，每个线程都可以独立地执行特定的任务。线程共享进程的内存空间和资源，可以更高效地完成并发执行的任务。线程之间可以共享数据和通信，但也需要合适的同步机制来保证数据的一致性和避免竞态条件。</li>
</ol>
<p>总的来说，程序是一组指令的集合，进程是程序在执行时的实例，而线程是进程内部的执行路径，用于并发执行任务。进程提供了资源分配和管理的环境，而线程提供了更细粒度的并发控制和共享资源的机制。</p>
<h1 id="创建线程的方式"><a href="#创建线程的方式" class="headerlink" title="创建线程的方式"></a>创建线程的方式</h1><p>在Java中，创建线程有四种方式：</p>
<ol>
<li>使用<code>Thread</code>类的子类：Java提供了<code>Thread</code>类，可以通过继承<code>Thread</code>类并重写<code>run()</code>方法来创建线程。</li>
<li>使用<code>Runnable</code>接口：Java中的线程类可以实现<code>Runnable</code>接口，并在<code>run()</code>方法中定义线程的逻辑。这种方式更加灵活，可以避免类的单继承限制。</li>
<li>使用匿名类：可以使用匿名类的方式直接创建线程，并重写<code>run()</code>方法。</li>
<li>使用<code>Lambda</code>表达式：在Java 8及以上版本中，可以使用<code>Lambda</code>表达式来创建线程。</li>
</ol>
<p>重点掌握前两种即可，下面详细介绍一下各种方式创建线程。</p>
<h2 id="使用Thread类的子类创建线程"><a href="#使用Thread类的子类创建线程" class="headerlink" title="使用Thread类的子类创建线程"></a>使用<code>Thread</code>类的子类创建线程</h2><p>在Java中，可以使用<code>Thread</code>类来创建和管理线程，下面是使用<code>Thread</code>类创建线程的一般步骤。</p>
<p>首先，创建一个继承自Thread类的子类。在子类中重写Thread类的run()方法，该方法包含了线程要执行的代码。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">MyThread</span> <span class="keyword">extends</span> <span class="title class_">Thread</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 线程执行的代码</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>接着需要创建线程对象。在主程序或其他类中，实例化Thread子类的对象。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">MyThread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyThread</span>();</span><br></pre></td></tr></tbody></table></figure>
<p>最后启动线程，调用线程对象的<code>start()</code>方法，以启动线程并开始执行<code>run()</code>方法中的代码。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">thread.start();</span><br></pre></td></tr></tbody></table></figure>
<h2 id="使用Runnable接口创建线程"><a href="#使用Runnable接口创建线程" class="headerlink" title="使用Runnable接口创建线程"></a>使用<code>Runnable</code>接口创建线程</h2><p>相比于继承<code>Thread</code>类，实现<code>Runnable</code>接口有以下优势：</p>
<ol>
<li>避免类的单继承限制：Java中一个类只能继承一个父类，但是可以实现多个接口。使用<code>Runnable</code>接口可以在不受单继承限制的情况下创建线程。</li>
<li>提高代码的可重用性：通过实现<code>Runnable</code>接口，将任务逻辑与线程类的定义分离开来，使得任务可以在不同的线程中重复使用。</li>
</ol>
<p>下面是使用<code>Runnable</code>接口创建线程的一般步骤。</p>
<p>首先，创建一个实现<code>Runnable</code>接口的类，并实现<code>run()方法</code>，该方法包含了线程要执行的代码。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">MyRunnable</span> <span class="keyword">implements</span> <span class="title class_">Runnable</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 线程执行的代码</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p> 接着创建一个线程对象，并将<code>Runnable</code>对象作为参数传递给<code>Thread</code>类的构造函数。</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Runnable</span> <span class="variable">myRunnable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Runnable</span>();</span><br><span class="line"><span class="type">Thread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(myRunnable);</span><br></pre></td></tr></tbody></table></figure>
<p>最后，启动线程。调用线程对象的<code>start()</code>方法，以启动线程并开始执行<code>run()</code>方法中的代码。</p>
<h2 id="使用匿名类创建线程"><a href="#使用匿名类创建线程" class="headerlink" title="使用匿名类创建线程"></a>使用匿名类创建线程</h2><p>下面是一个使用匿名类创建线程的示例：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MainClass</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="comment">// 使用匿名类创建线程</span></span><br><span class="line">        <span class="type">Thread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Runnable</span>() {</span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> {</span><br><span class="line">                <span class="comment">// 线程执行的代码</span></span><br><span class="line">            }</span><br><span class="line">        });</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 启动线程</span></span><br><span class="line">        thread.start();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="使用Lambda表达式创建线程"><a href="#使用Lambda表达式创建线程" class="headerlink" title="使用Lambda表达式创建线程"></a>使用<code>Lambda</code>表达式创建线程</h2><p>下面是一个使用lambda表达式创建线程的示例：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="comment">// 使用lambda表达式创建线程</span></span><br><span class="line">        <span class="type">Thread</span> <span class="variable">thread</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(() -&gt; {</span><br><span class="line">            <span class="comment">// 线程的逻辑代码</span></span><br><span class="line">            System.out.println(<span class="string">"线程正在运行"</span>);</span><br><span class="line">        });</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 启动线程</span></span><br><span class="line">        thread.start();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h1 id="与线程相关的方法"><a href="#与线程相关的方法" class="headerlink" title="与线程相关的方法"></a>与线程相关的方法</h1><h2 id="获取当前线程"><a href="#获取当前线程" class="headerlink" title="获取当前线程"></a>获取当前线程</h2><p>可以通过调用静态方法<code>Thread.currentThread()</code>获取当前线程。</p>
<h1 id="线程的状态"><a href="#线程的状态" class="headerlink" title="线程的状态"></a>线程的状态</h1><p>在Java中，线程可以有下面6种状态：</p>
<ol>
<li><strong>New（新建）</strong>：当线程对象被创建时，它处于新建状态。此时，线程还没有启动，尚未调用<code>start</code>方法。</li>
<li><strong>Runnable（可运行）</strong>：当线程启动后，它进入可运行状态。在可运行状态下，线程可能正在执行，也可能等待操作系统的调度以获得执行时间。</li>
<li><strong>Blocked（阻塞）</strong>：一个线程可能被阻塞，无法继续执行。当线程在等待某个监视器锁时，它会进入阻塞状态。例如，当一个线程调用了一个同步方法，而该方法已被其他线程占用时，该线程将被阻塞。</li>
<li><strong>Waiting（等待）</strong>：线程在等待某个特定条件发生时，可能进入等待状态。一个线程可以通过调用<code>Object</code>类的<code>wait</code>方法或线程的<code>join</code>方法进入等待状态。</li>
<li><strong>Timed Waiting（计时等待）</strong>：线程在等待一段指定的时间时，可能进入计时等待状态。一个线程可以通过调用<code>Thread</code>类的<code>sleep</code>方法、<code>Object</code>类的<code>wait</code>方法（带超时参数）或线程的<code>join</code>方法（带超时参数）进入计时等待状态。</li>
<li><strong>Terminated（终止）</strong>：线程的生命周期结束，处于终止状态。线程可能因为执行完毕、抛出异常或被中断而终止。</li>
</ol>
<p>下面详细介绍一下这几种状态。</p>
<h2 id="新建线程"><a href="#新建线程" class="headerlink" title="新建线程"></a>新建线程</h2><p>当新建一个线程，但是还没有调用该线程的<code>start()</code>方法，此时该线程的状态就是<strong>新建（new）</strong>。</p>
<h2 id="可运行线程"><a href="#可运行线程" class="headerlink" title="可运行线程"></a>可运行线程</h2><p>一旦调用<code>start()</code>方法，线程就处于<strong>可运行（Runnable）</strong>状态。一个可运行状态可能正在运行也可能没有运行。要由操作系统为线程提供具体的运行时间。</p>
<p>一个线程开始运行，它不一定始终保持运行。运行中的线程有时需要暂停，让其他线程有机会运行。</p>
<h2 id="阻塞和等待线程"><a href="#阻塞和等待线程" class="headerlink" title="阻塞和等待线程"></a>阻塞和等待线程</h2><p>当线程处于阻塞和等待状态时，它暂时不活动，不运行任何代码。</p>
<p><strong>==此处等待补充==</strong></p>
<h2 id="终止线程"><a href="#终止线程" class="headerlink" title="终止线程"></a>终止线程</h2><p>线程会由于以下两个原因之一而终止：</p>
<ul>
<li><code>run()</code>方法正常退出，线程自然终止。</li>
<li>因为一个没有捕获的异常终止了<code>run()</code>方法，使线程意外终止。</li>
</ul>
<h1 id="线程属性"><a href="#线程属性" class="headerlink" title="线程属性"></a>线程属性</h1><h2 id="中断线程"><a href="#中断线程" class="headerlink" title="中断线程"></a>中断线程</h2><p>原先有一个<code>stop()</code>方法可以强制终止一个线程，但现在这个方法已经废弃了，除了这个废弃的方法，没有办法可以<strong>强制终止</strong>一个线程。线程只能通过上一小节说的两个原因其中之一，让线程自然终止。</p>
<p>不过，<code>interrupt()</code>方法可以用来<strong>请求</strong>终止一个线程。当对一个线程调用该方法时，就会设置线程的<strong>中断状态</strong>。这是每个线程都有的<code>bollean</code>标志。每个线程都应该时不时地检查这个标志，以判断线程是否被中断。</p>
<p>可以通过<code>isInterrupted()</code>方法得出当前线程是否设置了中断状态：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(!Thread.currentThread().isInterrupted()) {</span><br><span class="line">    <span class="comment">// 要执行的代码</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>要注意的是，<strong>如果线程被阻塞就无法检查中断状态</strong>。这里就要引入<code>InterruptedException</code>异常。当在一个被<code>sleep()</code>或<code>wait()</code>调用阻塞的线程上调用<code>interrupt()</code>方法时，阻塞调用（即<code>sleep()</code>或<code>wait()</code>）将被一个<code>InterruptedException</code>异常中断，并进入<code>Runnable</code>状态，重新排队等待CPU资源。</p>
<p>例如，假设一个线程在执行<code>Thread.sleep(5000)</code>时被阻塞，即线程将休眠5秒钟。如果我们在另一个线程中调用阻塞线程的<code>interrupt()</code>方法，阻塞线程会立即抛出<code>InterruptedException</code>异常，而不是继续等待5秒钟，并且继续排队等待CPU资源。</p>
<h3 id="将中断解释为终止"><a href="#将中断解释为终止" class="headerlink" title="将中断解释为终止"></a>将中断解释为终止</h3><p>我们通常希望在使用<code>interrupt()</code>终止一个线程，而不是设置该线程的中断状态，那么可以像下面这样写<code>run()</code>方法：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Runnable</span> <span class="variable">r</span> <span class="operator">=</span> () -&gt; {</span><br><span class="line">    <span class="keyword">try</span> {</span><br><span class="line">        <span class="keyword">while</span>(!Thread.currentThread().isInterrupted()) {</span><br><span class="line">            <span class="comment">// 执行代码</span></span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">    <span class="keyword">catch</span>(InterruptedException w) {</span><br><span class="line">        <span class="comment">// 线程被sleep或wait阻塞，无法检查中断状态</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>在上面的<code>run()</code>方法中，在执行<code>try</code>块内的代码时需要先检查线程的中断状态，如果设置了中断状态，就不执行操作，线程自然终止。如果该线程被<code>sleep()</code>或<code>wait()</code>阻塞，就无法检查中断状态，也就无法达到通过设置中断来终止线程的效果。</p>
<h3 id="调用sleep就不要检测中断状态"><a href="#调用sleep就不要检测中断状态" class="headerlink" title="调用sleep就不要检测中断状态"></a>调用sleep就不要检测中断状态</h3><p>调用了<code>sleep()</code>方法之后（或者其他可中断方法），<code>isInterrupted()</code>检查既没必要也没有用处。</p>
<p>因为设置了中断状态后，调用<code>sleep()</code>方法并不会休眠，而是会清除中断状态并抛出<code>InterruptedException</code>异常。</p>
<h2 id="线程名"><a href="#线程名" class="headerlink" title="线程名"></a>线程名</h2><p>可以用<code>setName()</code>方法为线程设置任何名字：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">t</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(runnable);</span><br><span class="line">t.setName(<span class="string">"ThreadName"</span>);</span><br></pre></td></tr></tbody></table></figure>
<h2 id="未捕获异常的处理器"><a href="#未捕获异常的处理器" class="headerlink" title="未捕获异常的处理器"></a>未捕获异常的处理器</h2><p>线程的<code>run()</code>方法不能抛出任何检查型异常，但是，非检查型异常可能会导致线程终止，这会导致线程死亡。</p>
<p>实际上，在线程死亡之前，异常会传递到一个用于处理未捕获异常的处理器。</p>
<p>这个处理器必须属于一个实现了<code>Thread.UncaughtExceptionHandler</code>接口的类。这个接口只有一个方法：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">uncaughtException</span><span class="params">(Thread t,Throwable e)</span></span><br></pre></td></tr></tbody></table></figure>
<h1 id="同步"><a href="#同步" class="headerlink" title="同步"></a>同步</h1><p>大多数多线程应用中，两个或两个以上线程需要共享对同一数据的存取。如果两个线程同时存取同一对象，并且每个线程分别调用了一个修改该对象状态的方法，这两个线程会相互覆盖，可能会导致对象的破坏。这种情况称为<strong>静态条件</strong>。</p>
<p>为了避免多线程破坏共享数据，必须学会如何<strong>同步存取</strong>。</p>
<h2 id="锁对象"><a href="#锁对象" class="headerlink" title="锁对象"></a>锁对象</h2><h3 id="锁的使用结构"><a href="#锁的使用结构" class="headerlink" title="锁的使用结构"></a>锁的使用结构</h3><p>Java5提供了一个<code>ReentrantLock</code>类来防止并发访问代码块，用<code>ReentrantLock</code>保护代码块的基本结构如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">myLock.lock();</span><br><span class="line"><span class="keyword">try</span> {</span><br><span class="line">    </span><br><span class="line">}</span><br><span class="line"><span class="keyword">finally</span> {</span><br><span class="line">    myLock.unlock();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>这个锁是在要被访问的代码块中的，而不是放在线程中。</p>
<h3 id="锁的使用举例"><a href="#锁的使用举例" class="headerlink" title="锁的使用举例"></a>锁的使用举例</h3><p>下面来看一个使用锁来保护<code>Bank</code>类的<code>transfer()</code>方法：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Bank</span> {</span><br><span class="line">    <span class="keyword">private</span> <span class="type">var</span> <span class="variable">bankLock</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ReentrantLock</span>();</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">transfer</span><span class="params">(<span class="type">int</span> form,<span class="type">int</span> to,<span class="type">int</span> amount)</span> {</span><br><span class="line">        bankLock.lock();</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            System.out.println(Thread.currentThread());</span><br><span class="line">            accounts[form] - = amount;</span><br><span class="line">            accounts[to] += amount;</span><br><span class="line">        }</span><br><span class="line">        <span class="keyword">finally</span> {</span><br><span class="line">            bankLock.unlock();</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>假设一个线程调用了<code>transfer()</code>，但是在执行结束前被强占。再假设第二个线程也调用<code>transfer()</code>方法，由于第二个线程不能获得锁，将在调用<code>lock()</code>方法时被阻塞。它会暂停，必须等待第一个线程执行完<code>transfer()</code>方法。当第一个线程释放锁时，第二个线程才能开始运行。</p>
<h3 id="锁的嵌套调用与持有计数"><a href="#锁的嵌套调用与持有计数" class="headerlink" title="锁的嵌套调用与持有计数"></a>锁的嵌套调用与持有计数</h3><p>这个锁称为重入（reentrant）锁，因为线程可以反复获得已拥有的锁。锁有一个<strong>持有计数（hold count）</strong>来跟踪对<code>lock()</code>方法的嵌套调用。例如，<code>transfer()</code>方法调用<code>getTotalBalance()</code>方法，这个方法也会封锁<code>bankLock</code>对象，此时<code>bankLock</code>对象的持有计数为2，当<code>getTotalBalance()</code>方法退出时，持有计数变回1。当<code>transfer()</code>方法退出时，持有计数变为0，线程释放锁。</p>
<h3 id="条件对象"><a href="#条件对象" class="headerlink" title="条件对象"></a>条件对象</h3><p>通常，线程进入临界区后却发现只有满足某个条件之后它才能执行。可以使用一个条件对象来管理那些已经获得了一个锁却不能做有用工作的线程。</p>
<p>继续来看一个<code>Bank</code>类存在的问题，如果一个账户没有足够的资金转账，一般使用的是下面这样的判断逻辑:</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(bank.getBalance(from) &gt;= amount) {</span><br><span class="line">    bank.transfer(from,to.amount);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>上面的逻辑存在一个问题，当通过<code>if</code>判断后，假如线程立刻被中断，且中断后其他线程将待转出存款的账户余额修改成低于<code>amount</code>的数值，之后继续执行<code>transfer</code>，但账户余额却不足以转款操作了。</p>
<p>可以使用条件对象优化上面的判断逻辑。</p>
<p>一个锁对象可以有一个或多个相关联的条件对象。可以用<code>ReentrantLock.newCondition()</code>方法获得一个条件对象（<code>Condition</code>）。当某个条件不通过时，可以调用<code>Condition.await()</code>方法使当前线程暂停，并放弃锁。这就允许其他线程执行，不满足继续执行条件而暂停的线程将会进入这个条件的<strong>等待集（wait set）</strong>，并期望其他线程的操作可以使得不满足的条件得到满足。当另一个线程完成对等操作后，需要调用<code>Condition.signalAll()</code>方法，这会重新激活等待这个条件的所有线程。这些线程会尝试重新进入该对象，一旦锁可用，它们中的某个线程将从<code>await()</code>调用返回，得到这个锁，<strong>从之前暂停的地方继续执行</strong>。</p>
<p>通常，<code>await()</code>调用应该放在如下形式的循环中：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(!(OK to proceed)) </span><br><span class="line">    condition.await();</span><br></pre></td></tr></tbody></table></figure>
<p>下面我们来看一个例子，利用条件对象来完善转账功能：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Bank</span> {</span><br><span class="line">    <span class="keyword">private</span> <span class="type">var</span> <span class="variable">bankLock</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ReentrantLock</span>();</span><br><span class="line">    <span class="keyword">private</span> Contition sufficientFunds;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Bank</span><span class="params">()</span> {</span><br><span class="line">        sufficientFunds = bankLock.newCondition();</span><br><span class="line">    }</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">transfer</span><span class="params">(<span class="type">int</span> form,<span class="type">int</span> to,<span class="type">int</span> amount)</span> {</span><br><span class="line">        bankLock.lock();</span><br><span class="line">        <span class="keyword">try</span> {</span><br><span class="line">            <span class="keyword">while</span>(accounts[from] &lt; amount)</span><br><span class="line">                sufficientFunds.await(); <span class="comment">// 不满足条件，进入等待集</span></span><br><span class="line">            System.out.println(Thread.currentThread());</span><br><span class="line">            accounts[form] - = amount;</span><br><span class="line">            accounts[to] += amount;</span><br><span class="line">            sufficientFunds.signalAll(); <span class="comment">// 激活其他线程</span></span><br><span class="line">        }</span><br><span class="line">        <span class="keyword">finally</span> {</span><br><span class="line">            bankLock.unlock();</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="synchronized关键字"><a href="#synchronized关键字" class="headerlink" title="synchronized关键字"></a>synchronized关键字</h2><p><code>Lock</code>和<code>Condition</code>接口的使用很麻烦，可以使用Java的另一种规则。Java每个对象都有一个内部锁。</p>
<p>如果一个方法声明时有<code>synchronized</code>关键字，那么对象的锁将保护整个方法。也就是说，要调用这个方法，进程必须获得内部对象锁。</p>
<p>内部对象锁只有一个关联条件。<code>wait()</code>方法将一个线程增加到<strong>等待集</strong>中，<code>notifyAll()</code>/<code>notify()</code>方法可以解除等待线程的阻塞。</p>
<p>例如，可以如下实现<code>Bank</code>类：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Bank</span> {</span><br><span class="line">    <span class="keyword">private</span>[] accounts;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title function_">transfer</span><span class="params">(<span class="type">int</span> from,<span class="type">int</span> to,<span class="type">int</span> amount)</span></span><br><span class="line">    	<span class="keyword">throws</span> InterruptedException</span><br><span class="line">    {</span><br><span class="line">        <span class="keyword">while</span>(accounts[from] &lt; amount)</span><br><span class="line">            wait();</span><br><span class="line">        accounts[from] -= amount;</span><br><span class="line">        accounts[to] += amount;</span><br><span class="line">        notifyAll();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Java%E5%9F%BA%E7%A1%80-%E5%A4%9A%E7%BA%BF%E7%A8%8B-Java%E5%A4%9A%E7%BA%BF%E7%A8%8B/" rel="tag"># -Java基础 -多线程 -Java多线程</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/06/10/WEB%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B8%88%E8%AF%BE%E7%AC%94%E8%AE%B0/" rel="prev" title="WEB前端大师课笔记">
                  <i class="fa fa-chevron-left"></i> WEB前端大师课笔记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/06/13/CPU/" rel="next" title="">
                   <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
